//index.js
//获取应用实例
const app = getApp()


Page({
    data: {
        imgUrls: [
            '../../images/1.png',
            '../../images/2.png',
            '../../images/3.png',
        ],
        animation1: '',
        animation2: '',
        animation3: '',
        indicatorDots: false,
        autoplay: true,
        interval: 5000,
        duration: 1000,
        second: 0,
        swiperIndex: 0,
        touchDot: 0,
        timeIn:null,
        news: [
            '../../images/test.png',
            '../../images/test1.png',
            '../../images/test3.png'
        ],
        time: 0,
        done:false,
        data: [
            {
                image:'../../images/template.png',
                title:'给5000万农名工上社保！税务局已经准备好了',
                type:'#建筑新闻',
                time:'2018.1.1'
            },
            {
                image:'../../images/template.png',
                title:'给5000万农名工上社保！税务局已经准备好了',
                type:'#建筑新闻',
                time:'2018.1.1'
            },
            {
                image:'../../images/template.png',
                title:'给5000万农名工上社保！税务局已经准备好了',
                type:'#建筑新闻',
                time:'2018.1.1'
            },
            {
                image:'../../images/template.png',
                title:'给5000万农名工上社保！税务局已经准备好了',
                type:'#建筑新闻',
                time:'2018.1.1'
            },
            {
                image:'../../images/template.png',
                title:'给5000万农名工上社保！税务局已经准备好了',
                type:'#建筑新闻',
                time:'2018.1.1'
            },
        ],
        page:1,
        loadData:false,
        empty:true,
        end:false,
        temp:[]
    },
    zhihui() {
        wx.navigateTo({
            url: '/pages/zhihui/index'
        })
    },
    living() {
        app.axios('/info', 'GET', response => {
            if (response) {
                console.log(response.type)
                //验证是否为项目负责人
                if (response.type === 2) {
                    //TODO
                    if (response) {
                        wx.navigateTo({
                            url: '/pages/project/index'
                        })
                    }
                }

                else if (response.type === 1) {
                    wx.showModal({
                        title: '身份错误',
                        content: '您不是项目人员，无法查看',
                        showCancel: false
                    })
                }
                else {
                    wx.showModal({
                        title: '信息错误',
                        content: '您尚未完善相关信息，请先完善信息',
                        showCancel: false,
                        success: res => {
                            wx.switchTab({
                                url: '/pages/main/index'
                            })
                        }
                    })
                }

            }
        });
        // if (!app.globalData.userToken) {
        //     wx.switchTab({
        //         url: '/page/main/index'
        //     })
        // }else{
        //     wx.navigateTo({
        //         url: '/pages/team/index'
        //     })
        // }
    },
    project() {
        app.axios('/info', 'GET', response => {
            if (response) {
                //验证是否为班组成员
                if (response.type === 2) {
                    wx.showModal({
                        title: '身份错误',
                        content: '您不是施工人员，无法查看',
                        showCancel: false
                    })
                }
                else if (response.type === 1) {
                    wx.navigateTo({
                        url: '/pages/items/index'
                    })
                }
                else {
                    wx.showModal({
                        title: '信息错误',
                        content: '您尚未完善相关信息，请先完善信息',
                        showCancel: false,
                        success: res => {
                            wx.switchTab({
                                url: '/pages/main/index'
                            })
                        }
                    })

                }

            }
        });
    },
    onLoad: function () {
        // this.scoreLeft()
        var that = this;
        wx.getSystemInfo({
            success: res => {
                this.setData({
                    second: res.windowHeight - res.windowWidth / 750 * 300
                })
            }
        })
        this.setTime()
        this.getData(this.data.page)
    },
    swiperChange(e) {
        this.setData({
            swiperIndex: e.detail.current
        })
    },
    touchstart(e) {
        // console.log(e.touches[0].pageX);
        this.data.touchDot = e.touches[0].pageX
        var that = this;
        this.data.interval = setInterval(function () {
            that.data.time += 1;
        }, 100);
    },
    setTime(){
        if (this.data.timeIn){
            clearInterval(this.data.timeIn)
        }
        let that = this
        this.data.timeIn = setInterval(function () {
            that.scoreLeft()
        },3000)
    },
    touchmove(e) {
        let touchMove = e.touches[0].pageX
        let touchDot = this.data.touchDot
        let time = this.data.time
        console.log(touchMove - touchDot && time<10 &&!this.data.done)
        if (touchMove - touchDot > 30) {
            this.data.done = true;
            // this.scoreLeft()
        }
        if (touchMove - touchDot < -30 && time < 10 && !this.data.done) {
            this.data.done = true;
            this.scoreLeft()
        }
    },
    touchend(e) {
        clearInterval(this.data.interval);
        this.setTime()
        this.data.time = 0;
        this.data.done = false;
    },
    scoreLeft() {
        console.log(1111)
        let animation1 = wx.createAnimation({
            duration: 300,
            timingFunction: "easy",
            delay: 0,
            transformOrigin: '50% 50%'
        })
        let animation2 = wx.createAnimation({
            duration: 300,
            timingFunction: "easy",
            delay: 0,
            transformOrigin: '50% 50%'
        })
        let animation3 = wx.createAnimation({
            duration: 300,
            timingFunction: "easy",
            delay: 0,
            transformOrigin: '50% 50%'
        })
        animation1.opacity(0).step();
        animation2.scale(1).opacity(1).translateX(-38).step();
        animation3.scale(0.8).opacity(0.8).translateX(-46).step();
        this.setData({
            animation1: animation1.export(),
            animation2: animation2.export(),
            animation3: animation3.export(),
        })

        const that = this
        setTimeout(function () {
            animation1.opacity(1).step({duration: 0, timingFunction: 'linear'});
            animation2.opacity(0.8).scale(0.8).translateX(2).step({duration: 0, timingFunction: 'linear'});
            animation3.scale(0.6).opacity(0.6).translateX(2).step({duration: 0, timingFunction: 'linear'});
            that.setData({
                animation1: animation1.export(),
                animation2: animation2.export(),
                animation3: animation3.export()
            })
        }.bind(this), 195)

        let array = this.data.news;
        let shift = array.shift();
        array.push(shift);

        setTimeout(function () {
            this.setData({
                news: array
            })
        }.bind(this), 195)
    },

    onPullDownRefresh (){
        this.setData({
            page:1,
            temp:[],
            empty:true,
            end:false
        })
        this.getData(this.data.page)
        wx.stopPullDownRefresh()
    },
    getData(page){
        if (!this.data.loadData){
            let {page} = this.data
            this.setData({
                loadData:true
            })
            app.axios('/news','GET',response=>{
                let data = [...this.data.temp,...response]
                this.setData({
                    loadData:false,
                    temp:data
                })
            },{page})
        }

    },
    onReachBottom(){
        if (!this.data.end){
            this.setData({
                page:this.data.page+1
            })
            this.getData(this.data.page)
        }
    },
    toDetails(e){
        wx.navigateTo({
            url: '/pages/news/index'+'?id='+e.currentTarget.dataset.id
        })
    },
})
